package com.bweab.controll.userstbcontrol;

import com.bweab.pojo.UsersTb;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/alterUser")
public class AlterUser extends HttpServlet {
    //创建服务对象
    UsersTbServiceInterface usersService=new UsersTbServiceInterfaceImpl();
    //创建返回对象
    JSONResult jsonResult = new JSONResult();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userphone");
        String userXh = req.getParameter("userxh");
        String userSex = req.getParameter("usersex");

        System.out.println(userName+"\n"+ userPhone+"\n"+ userXh+"\n"+userSex );
        if(userName==null || userPhone==null || userXh==null || userSex==null){
            //失败
            jsonResult.setStatusCode(JSONStats.USER_ALTER_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.USER_ALTER_ERROR.getMessage());
            WebUtil.writeJson(resp,jsonResult);
            return;
        }
        int rows=0;
        //调用服务层
        rows= usersService.alterUserInfo(userName, userPhone, userXh, userSex); //调用服务层修改

        //根据影响的行数判断是否修改成功
        if(rows<=0){
            //失败
                jsonResult.setStatusCode(JSONStats.USER_ALTER_ERROR.getStatusCode());
                jsonResult.setMessage(JSONStats.USER_ALTER_ERROR.getMessage());

        }else if(rows>0){
            //修改成功
            jsonResult.setStatusCode(JSONStats.USER_ALTER_OK.getStatusCode());
            jsonResult.setMessage(JSONStats.USER_ALTER_OK.getMessage());

        }

        //发送
        WebUtil.writeJson(resp,jsonResult);

    }
}
